﻿using System;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Cryptography;

namespace Solution.BusinessObject
{
    /// <summary>
    /// Provides the most method for common use.
    /// </summary>
    public static class CommonBO
    {
        /// <summary>
        /// Gets or sets default language of site.
        /// </summary>
        private static Language _defaultSiteLanguage;

        /// <summary>
        /// gets or set Master EDM Container of site.
        /// </summary>
        private static MasterEDMContainer _masterEDM;

        /// <summary>
        /// Gets or sets Master EDM Container.
        /// </summary>
        public static MasterEDMContainer MasterEDM
        {
            get
            {
                return _masterEDM ?? new MasterEDMContainer();
            }
            set { _masterEDM = value; }
        }

        private static SqlConnection _sqlConnection;
        public static SqlConnection SqlConnection
        {
            get
            {
                return _sqlConnection ??
                       new SqlConnection(System
                                             .Configuration
                                             .ConfigurationManager
                                             .ConnectionStrings["DefaultConnection"]
                                             .ConnectionString);
            }
            set { _sqlConnection = value; }
        }

        private static ObjectContext _objectContext;
        public static ObjectContext ObjectContext
        {
            get
            {
                return _objectContext ??
                       new ObjectContext(System.Configuration
                                             .ConfigurationManager
                                             .ConnectionStrings["MasterEDMContainer"]
                                             .ConnectionString);
            }
            set { _objectContext = value; }
        }

        /// <summary>
        /// Gets or sets site language.
        /// </summary>
        public static Language DefaultSiteLanguage
        {
            get
            {
                return _defaultSiteLanguage
                       ?? (_defaultSiteLanguage = MasterEDM.Languages
                                                      .FirstOrDefault(t => t.IsDefault == true));
            }
            set { _defaultSiteLanguage = value; }
        }

        /// <summary>
        /// Writes error exception to text file.
        /// </summary>
        public static void WriteLog(Exception exception)
        {
        }
        
        public static string EncryptMD5(string text)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] byteValues = System.Text.Encoding.Unicode.GetBytes(text);
            byte[] byteHash = md5.ComputeHash(byteValues);
            return Convert.ToBase64String(byteHash);
        }
    }
}
